package com.sunspring.zdcall.service.impl;

import com.google.common.collect.Lists;
import com.sunspring.zdcall.common.constant.enums.StateEnum;
import com.sunspring.zdcall.common.exception.ZdException;
import com.sunspring.zdcall.entity.ZdBaseCheckroom;
import com.sunspring.zdcall.entity.ZdBaseDepartment;
import com.sunspring.zdcall.mapper.master.DeptMapper;
import com.sunspring.zdcall.service.CheckRoomService;
import com.sunspring.zdcall.service.DeptService;
import com.sunspring.zdcall.support.cache.DeptCache;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @Author WuLei
 * @Date 2018/11/27  16:55
 */
@Service
@Slf4j
public class DeptServiceImpl implements DeptService {

    @Autowired
    private DeptMapper deptMapper;

    @Autowired
    private CheckRoomService crs;

    @Autowired
    private DeptCache deptCache;

    @Override
    public List<ZdBaseDepartment> getSelective(ZdBaseDepartment department) {
        return deptMapper.selectSelective(department);
    }

    @Override
    public void addDept(ZdBaseDepartment departmentInfo) {
        int result = deptMapper.insertDeptSelective(departmentInfo);
        if (result <= 0) {
            throw new ZdException("新增科室信息失败,科室信息,科室信息：" + departmentInfo);
        }
        log.info("新增科室，科室id{}",departmentInfo.getDepartmentId());
        deptCache.setDept(departmentInfo);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteDept(String deptId) {
        int result = deptMapper.deleteById(deptId);
        if (result <= 0) {
            throw new ZdException("删除科室信息失败");
        }
        //删除科室下的所有检查室
        crs.deleteByDeptId(deptId);
        log.info("删除科室，科室id{}",deptId);
        deptCache.removeDept(deptId);
    }

    @Override
    public void update(ZdBaseDepartment departmentInfo) {
        int result = deptMapper.updateById(departmentInfo);
        if (result <= 0){
            throw new ZdException("修改检查室信息失败，检查室id："+departmentInfo);
        }
        log.info("科室信息更新，科室id{}",departmentInfo.getDepartmentId());
        deptCache.setDept(departmentInfo);
    }

    @Override
    public ZdBaseDepartment selectById(String deptId) {
        ZdBaseDepartment department = deptCache.getDept(deptId);
        if (department != null) {
            return department;
        }
        department = new ZdBaseDepartment();
        department.setDepartmentId(deptId);
        List<ZdBaseDepartment> list = getSelective(department);
        if (!CollectionUtils.isEmpty(list)) {
            department = list.get(0);
            //存入缓存
            deptCache.setDept(department);
            return department;
        }
        return null;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void switchDept(String deptId) {
        int state = selectById(deptId).getDepartmentState();
        int newState = state == StateEnum.enable.getCode() ? StateEnum.disable.getCode() : StateEnum.enable.getCode();
        if (deptMapper.updateStateById(deptId,newState) <= 0) {
            throw new ZdException("更新科室状态失败");
        }
        //停用该科室下所有检查室
        if (newState == StateEnum.disable.getCode()
                && !CollectionUtils.isEmpty(crs.getByDeptId(deptId))) {
            crs.updateStateByDeptId(deptId,StateEnum.disable.getCode());
        }
        if (newState == StateEnum.enable.getCode()) {
            log.info("科室Id:{},已被启用",deptId);
        }
        if (newState == StateEnum.disable.getCode()) {
            log.info("科室Id:{},已被停用",deptId);
        }
        deptCache.updateDeptState(deptId,newState);
    }



}
